﻿Public Class Principal
    Dim _PreguntaSAlida As Boolean
    Dim _RutaResp = Vales_Verificador_3.Verificador._RutaResp

    Private Sub Principal_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
            If FuncionSalir() = False Then
                e.Cancel = True
                _PreguntaSAlida = True
                Return
            End If
        End If
        Application.Exit()
    End Sub
    Private Sub Principal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Verificador.MdiParent = Me
        Verificador.Show()
        Verificador.Focus()
    End Sub

    Private Sub SalirToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SalirToolStripMenuItem.Click
        If FuncionSalir() Then
            Application.Exit()
        End If
    End Sub

    Private Sub OrganizarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OrganizarToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub CambiarConexionToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CambiarConexionToolStripMenuItem.Click
        Dim ini As New IniFile
        AsistenteConexion.txtUserName.Text = "Sa"
        AsistenteConexion.txtPassword.Text = "admingas"
        AsistenteConexion.cmbDatabases.Text = "admingas"
        If AsistenteConexion.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            If IO.File.Exists(Verificador._RutaConexion) = True Then
                ini.Load(Verificador._RutaConexion)
            Else
                MsgBox("Error Al cargar la configuracion de conexion, la aplicacion se cerrara", MsgBoxStyle.Critical, "Error")
                End
            End If
            Dim _Instancia = ini.GetKeyValue("instancia", "instancia")
            My.Settings.RunTimeSqlConnectionString = "Server=" & _Instancia & ";Database=admingas;User Id=sa;Password=admingas;Timeout=60"
            My.Settings.Save()
            Dim cn As New SqlClient.SqlConnection("Server=" & _Instancia & ";Database=admingas;User Id=sa;Password=admingas;Timeout=10")
            Try
                cn.Open()
                cn.Close()
            Catch ex As Exception
                MsgBox(ex.Message & vbCrLf & cn.ConnectionString.ToString, MsgBoxStyle.Exclamation, "Error de conexion")
                AsistenteConexion.ShowDialog(Me)
            End Try
        End If
    End Sub

    Private Sub VerificadorValesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VerificadorValesToolStripMenuItem.Click
        Verificador.MdiParent = Me
        Verificador.Show()
        Verificador.Focus()
    End Sub

    Private Sub ReportesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReportesToolStripMenuItem.Click
        MenuReportes.MdiParent = Me
        MenuReportes.Show()
        MenuReportes.Focus()
    End Sub

    Private Sub BuscarFolioValeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BuscarFolioValeToolStripMenuItem.Click
        Form1.MdiParent = Me
        Form1.Show()
        Form1.Focus()
    End Sub

    Private Sub CerrarTodoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CerrarTodoToolStripMenuItem.Click
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
        Verificador.MdiParent = Me
        Verificador.Show()
        Verificador.Focus()
    End Sub

    Private Sub VentanaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VentanaToolStripMenuItem.Click
        'For Each ChildForm As Form In Me.MdiChildren
        'ChildForm.Close()
        'Next
    End Sub
    Public Sub RefreshWindowList()
        ' Count windows and Menuitems
        Dim mdiWindowCount As Integer = Me.MdiChildren.Count
        Dim menuItemWindowCount As Integer = Me.VentanaToolStripMenuItem.DropDownItems.Count

        ' Remove all WINDOW MenuItems from the Window menu, but don't remove the
        ' extra items such as Close and the separator (first two) and the last item
        Dim item As ToolStripItem
        For i As Integer = menuItemWindowCount - 1 To 0 Step -1
            item = Me.VentanaToolStripMenuItem.DropDownItems(i)

            If Not (item Is CerrarTodoToolStripMenuItem _
                    Or item Is ToolStripSeparator1 Or item Is OrganizarToolStripMenuItem) Then
                Me.VentanaToolStripMenuItem.DropDownItems.RemoveAt(i)
            End If
        Next

        If mdiWindowCount > 0 Then
            Dim menuItem As ToolStripMenuItem
            Dim counter As Integer = 1
            ' Add the new window items
            For Each window As Form In Me.MdiChildren
                'Create new menuitem
                menuItem = New ToolStripMenuItem

                'Set the text to for example "2. Windowtext"
                menuItem.Text = counter.ToString & ". " & window.Text

                'Set the tag to the current window so we can retrieve it later
                menuItem.Tag = window

                'Check the menuitem if this is the currently active window
                If window Is Me.ActiveMdiChild Then
                    menuItem.Checked = True
                End If

                'Add a Click EventHandler to be able to click it
                AddHandler menuItem.Click, AddressOf WindowMenuItemClicked

                'Finally add it to the actual menuitem list
                Dim Cont = IIf(Me.VentanaToolStripMenuItem.DropDownItems.Count - 1 >= 0, Me.VentanaToolStripMenuItem.DropDownItems.Count - 1, 0)
                Me.VentanaToolStripMenuItem.DropDownItems.Insert(Cont, menuItem)

                'Raise the counter by 1
                counter += 1
            Next
        End If
        'Me.VentanaToolStripMenuItem.DropDownItems.Insert(Me.VentanaToolStripMenuItem.DropDownItems.Count - 1, ToolStripSeparator1)
        'Me.VentanaToolStripMenuItem.DropDownItems.Insert(Me.VentanaToolStripMenuItem.DropDownItems.Count - 1, OrganizarToolStripMenuItem)
        'Me.VentanaToolStripMenuItem.DropDownItems.Insert(Me.VentanaToolStripMenuItem.DropDownItems.Count - 1, CerrarTodoToolStripMenuItem)
    End Sub

    Private Sub WindowMenuItemClicked(ByVal sender As Object, ByVal e As EventArgs)
        'Retrieve the clicked MenuItem from the sender object
        Dim menuItem As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)

        'Retrieve the corresponding form from the Tag property
        Dim frm As Form = TryCast(menuItem.Tag, Form)

        'Activate it
        If frm IsNot Nothing Then
            frm.Activate()
        End If
    End Sub

    Private Sub VentanaToolStripMenuItem_DropDownOpening(sender As Object, e As EventArgs) Handles VentanaToolStripMenuItem.DropDownOpening
        RefreshWindowList()
        'Me.VentanaToolStripMenuItem.DropDownItems.Insert(Me.VentanaToolStripMenuItem.DropDownItems.Count - 1, ToolStripSeparator1)
        'Me.VentanaToolStripMenuItem.DropDownItems.Insert(Me.VentanaToolStripMenuItem.DropDownItems.Count - 1, OrganizarToolStripMenuItem)
        'Me.VentanaToolStripMenuItem.DropDownItems.Insert(Me.VentanaToolStripMenuItem.DropDownItems.Count - 1, CerrarTodoToolStripMenuItem)
    End Sub

    Private Sub Menos30ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Menos30ToolStripMenuItem.Click
        'TimeOfDay = DateValue("00:33:00")
        Today = DateValue(Now.AddDays(-30))
    End Sub

    Private Sub Mas30ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Mas30ToolStripMenuItem.Click
        Microsoft.VisualBasic.DateAndTime.Today = Now.Date.AddDays(30)
        'Microsoft.VisualBasic.DateAndTime.TimeOfDay = Now.Date.AddDays(30)
    End Sub

    Private Sub RutaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RutaToolStripMenuItem.Click
        Process.Start(Verificador._RutaResp)
    End Sub
End Class